<?php


namespace services\shop;

use common\components\Service;
use common\helpers\CommonHelper;
use common\models\shop\Shop;
use Yii;

class ShopService extends Service
{
    /**
     * 根据Ids获取列表【禁用 + 正常】
     * @param array|null $ids
     * @return Shop[]
     */
    public static function getNormalListByIds(?array $ids = null, $orId = []): array
    {
        $query = Shop::find()
            ->select(['*'])
            ->where([
                'status' => [Shop::STATUS_INVALID, Shop::STATUS_ENABLE],
            ]);
        if (!is_null($ids)) {
            $query->andWhere(['id' => $ids]);
        }
        if ($orId) {
            $query->orWhere([
                'id' =>$orId,
                'status' => [Shop::STATUS_INVALID, Shop::STATUS_ENABLE],
            ]);
        }
        return $query->orderBy(['id' => SORT_ASC])->all();
    }

    /**
     * 获取正常
     * @param array|int|null $orIds
     * @return Shop[]
     */
    public static function getEnableListByIds($orIds = null): array
    {
        $query = Shop::find()
            ->select(['*'])
            ->where(['status' => Shop::STATUS_ENABLE]);
        if (!is_null($orIds)) {
            $query->orWhere(['id' => $orIds, 'status' => [Shop::STATUS_INVALID, Shop::STATUS_ENABLE]]);
        }
        return $query->orderBy(['id' => SORT_ASC])->all();
    }

    /**
     * 获取列表筛选项
     * @return array
     */
    public static function getListCondition(): array
    {
        return CommonHelper::getListCondition(self::getNormalListByIds(), ['value_column' => 'id', 'label_column' => 'name']);
    }

    /**
     * 获取编辑筛选项
     * @param int $type
     * @param int|array|null $orIds
     * @return array
     */
    public static function getEditCondition($orIds = null): array
    {
        return CommonHelper::getListCondition(self::getEnableListByIds($orIds), ['value_column' => 'id', 'label_column' => 'name']);
    }
}
